package com.hudson.plugin.result.collector.server.javaProcessor.internal;

import com.hudson.plugin.common.PluginInterface;
import org.apache.camel.CamelContext;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.WireTapDefinition;
import org.springframework.osgi.service.importer.OsgiServiceLifecycleListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Richard
 * Date: 4/24/11
 * Time: 10:46 PM
 * To change this template use File | Settings | File Templates.
 */
public class PluginBundleListener implements OsgiServiceLifecycleListener {
    private CamelContext camelContext;
    private List<PluginInterface> services;

    public void bind(Object o, Map map) throws Exception {

        for (PluginInterface service : getServices()) {

            List<ProcessorDefinition> tos = new ArrayList<ProcessorDefinition>();
            Map<String, String> properties = service.getPluginProperties();
            //make the toDefinition
            WireTapDefinition to = new WireTapDefinition();
            if (properties.get("endPoinrURL") != null) {
                to.setUri(properties.get("endPoinrURL"));
                to.setId(String.valueOf(map.get("Bundle-SymbolicName")));
                tos.add(to);
            }


            //add the toDefinition for multiCast
//            RouteDefinition dataRoute = camelContext.getRouteDefinition("hudsonDataRouter");
//            List<ProcessorDefinition> outputs = dataRoute.getOutputs();
//            for (int i = 0; i < outputs.size(); i++) {
//                if (dataRoute.getOutputs().get(i).getId().equals("multiCast")) {
//                    dataRoute.getOutputs().get(i).getOutputs().add(to);
//                    break;
//                }
//            }

            //add the toDefinition for wiretap  , note there will be a error if we don't convert to the map to map from json
            RouteDefinition dataRoute = camelContext.getRouteDefinition("hudsonDataRouter");
            List<ProcessorDefinition> outputs = dataRoute.getOutputs();
            List<ProcessorDefinition> tempOutputs = new ArrayList<ProcessorDefinition>();
            tempOutputs.add(to);
            tempOutputs.addAll(outputs);
            dataRoute.setOutputs(tempOutputs);


            //refresh the route
            camelContext.removeRouteDefinition(dataRoute);
            camelContext.addRouteDefinition(dataRoute);


        }


    }

    public void unbind(Object o, Map map) throws Exception {

        //remove the toDefinition for multicast
//        RouteDefinition dataRoute = camelContext.getRouteDefinition("hudsonDataRouter");
//        List<ProcessorDefinition> outputs = dataRoute.getOutputs();
//        for (int i = 0; i < outputs.size(); i++) {
//            if (outputs.get(i).getId().equals("multiCast")) {
//                for (int j = 0; j < outputs.get(i).getOutputs().size(); j++) {
//                    if (((ProcessorDefinition) outputs.get(i).getOutputs().get(j)).getId().equals(String.valueOf(map.get("Bundle-SymbolicName")))) {
//                        dataRoute.getOutputs().get(i).getOutputs().remove(j);
//                        break;
//                    }
//                }
//                break;
//            }
//        }

        //remove the toDefinition for wiretap
        RouteDefinition dataRoute = camelContext.getRouteDefinition("hudsonDataRouter");
        List<ProcessorDefinition> outputs = dataRoute.getOutputs();
        for (int i = 0; i < outputs.size(); i++) {
            if (outputs.get(i).getId().equals(String.valueOf(map.get("Bundle-SymbolicName")))) {
                dataRoute.getOutputs().remove(i);
                break;
            }
        }


        //refresh the route
        camelContext.removeRouteDefinition(dataRoute);
        camelContext.addRouteDefinition(dataRoute);

    }

    public CamelContext getCamelContext() {
        return camelContext;
    }

    public void setCamelContext(CamelContext camelContext) {
        this.camelContext = camelContext;
    }

    public List<PluginInterface> getServices() {
        return services;
    }

    public void setServices(List<PluginInterface> services) {
        this.services = services;
    }
}
